package Core;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;

public class FormManager extends GUIObject
{
	Vector forms = new Vector();
	int lastFSForm = 0;

	public FormManager(int w, int h)
	{
		super(w, h);
	}
	
	public void addForm(Form f)
	{
		forms.addElement(f);
		
		for(lastFSForm = forms.size()-1; lastFSForm>0; lastFSForm--) if(((Form)forms.elementAt(lastFSForm)).isFullscreen) break;

		f.onEvent(GM.FORM_OPENED, f);
		//System.out.println("Form "+f.getName()+" opened");
	}

	public void processKey(int key, int state)
	{
		if(forms.isEmpty()) return;
		((Form)forms.lastElement()).processKey(key, state);
	}

	public void draw(Graphics g, int x, int y)
	{
		Form form;
		for(int i=lastFSForm; i<forms.size(); i++)
		{
			form = (Form)forms.elementAt(i);
			
			if(form.isFullscreen) form.draw(g, x, y);
			else
			{
				if((form.style & Form.NO_CENTRALISE) == Form.NO_CENTRALISE) form.draw(g, x+form.getCoord().x, y+form.getCoord().y);
				else if((form.style & Form.CENTRALISE_FROM_BG) == Form.CENTRALISE_FROM_BG)
				{
					form.draw(g, x+(getW()-form.form.bg.getW())/2, y+(getH()-form.form.bg.getH())/2);
				}
				else form.draw(g, x+(getW()-form.getW())/2, y+(getH()-form.getH())/2);
			}
		}
	}

	void closeUpperForm()
	{
		if(!forms.isEmpty())
		{
			Form f = (Form) forms.lastElement();
			forms.removeElementAt(forms.size()-1);

			//if(forms.size() >= 2) ((Form)forms.elementAt(forms.size()-2)).onEvent(GM.UPPER_FORM_CLOSED, forms.lastElement());
			
			
			if(!forms.isEmpty()) ((Form)forms.lastElement()).onEvent(GM.UPPER_FORM_CLOSED, f);

		}


		for(lastFSForm = forms.size()-1; lastFSForm>0; lastFSForm--) if(((Form)forms.elementAt(lastFSForm)).isFullscreen) break;
	}
	
}
